package com.jm3.riscar3.commands;

import com.jm3.riscar3.core.Microcontroller;
import com.jm3.riscar3.helpers.BinaryTranslator;
import com.jm3.riscar3.interfaces.IAR3Command;
import com.jm3.riscar3.memory.Register;
import com.jm3.riscar3.memory.StatusRegister;

public class LDArf implements IAR3Command{

	private final String OpCode = "01010";
	private Register Accumulator;
	private Register[] Registers;
	private StatusRegister SR;
	
	public LDArf(Microcontroller mcu)
	{
		this.Accumulator = mcu.getCPU().getAcc();
		this.Registers = mcu.getCPU().getGRegs();
		this.SR = mcu.getCPU().getSR();
	}
	
	@Override
	public void execute(String args) throws Exception {
		// TODO Auto-generated method stub
		
		String regFromArgs = args.substring(0, 3);
		int regNumber = BinaryTranslator.getBitValue((regFromArgs));
		Accumulator.writeRegister(Registers[regNumber].readRegister());
		
		setFlags();
	}

	@Override
	public String getOpCode() {
		// TODO Auto-generated method stub
		return this.OpCode;
	}
	
	public void setFlags() throws Exception
	{
		int accValue = Accumulator.readRegister();
		boolean isZero = (accValue == 0);
		boolean isNegative = ((accValue & 128) != 0);
		
		SR.setZeroFlag(isZero);
		SR.setNegativeFlag(isNegative);
	}

}
